import request from '../../utils/request'

Page({
    data: {
        signDetail: {}, 
        userList: [],
        currentUser: null,
        paymentPageOptions: {}, 
        tagIds: [] // 新增存储tagid的数组
    },
    // 服务包价格查询
    getpayment(){
        console.log('1111');
        request({
            url: 'package/package/list',
            method: 'GET',
            success: (res) => {
                console.log(res.data.rows);
            },
            fail: (err) => {
                console.log('获取数据失败', err);
            }
        });
    },
    onLoad: function (options) {
        const {
            id,
            status
        } = options;
        console.log('接收到的 id:', id);
        console.log('接收到的原始 status:', status);
        console.log('接收到的 status 的类型:', typeof status);
        const parsedStatus = status
        console.log('解析后的 status:', parsedStatus);
        this.setData({
           signDetail2: {status:parsedStatus}
        });
        this.getSignDetail(id);
        this.getUserInfo();
        this.getpayment();
    },
    onShow() {
        const pages = getCurrentPages();
        const currentPage = pages[pages.length - 1];
        const { id } = currentPage.options;
        this.getSignDetail(id);
        this.getUserInfo();
        this.getpayment();
    },
    getSignDetail(id) {
        console.log('111111');

        request({
            url: 'contractlog/contractlog/list',
            method: 'GET',
            data: {pageNum:1,pageSize:100000,
                id: id
            },
            success: (res) => {
                const userId = wx.getStorageSync('userid');
                console.log('22222222222222222', res.data.rows);
                console.log(userId);
                
                // 遍历数组，找到userid和userId相等的记录
                const signDetail = res.data.rows.find(item => {
                    const itemUserId = item.user && item.user.id;
                    console.log('itemUserId:', itemUserId);
                    return itemUserId === userId;
                }) || {};
                
                signDetail.status = parseInt(signDetail.status, 10); 
                this.setData({
                    signDetail: signDetail
                });
                if (signDetail.status !== this.data.signDetail.status) {
                    this.setData({
                        'signDetail.status': signDetail.status
                    });
                }
                // 在获取到签约详情后，将价格传递到支付页面
                const paymentPageOptions = {
                    amount: signDetail.price,
                    signId: signDetail.id,
                    servicePackageName: signDetail.servicePackageName
                };
                this.setData({
                    paymentPageOptions: paymentPageOptions
                });
            },
            fail: (err) => {
                console.log('获取签约详情失败', err);
            }
        });
    },
    getUserInfo(userId = wx.getStorageSync('userid')) {
        request({
            url: 'resident/resident/list',
            method: 'GET',
            success: (res) => {
                console.log(res.data);
                const userList = res.data.rows;
                this.setData({
                    userList: userList
                });
                const currentUser = userList.find(item => item.id === userId);

                if (currentUser) {
                    this.setData({
                        currentUser: currentUser
                    });
                } else {
                    console.log('未找到当前用户');
                }
            },
            fail: (err) => {
                console.log('获取用户信息失败', err);
            }
        });
    },
    handlePay() {
        const { paymentPageOptions } = this.data;
        wx.navigateTo({
            url: `/pages/payment/payment?${Object.entries(paymentPageOptions).map(([key, value]) => `${key}=${value}`).join('&')}`
        });
    },
    reSubmitSign() {
        wx.navigateTo({
            url: `/pages/index/nearbyTeams/nearbyTeams`
        });
    },
    handleRenew: function() {
        // 处理续约逻辑
        wx.navigateTo({
            url: `/pages/index/nearbyTeams/nearbyTeams`
        });
    }
});        